package com.hunttown.mes.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * created by wangjunfu on 2018/11/7
 */
public class Html2TextUtils {

    /**
     * 过滤 Html 标签 返回纯文本
     *
     * @param inputString
     * @return
     */
    public static String Html2Text(String inputString) {
        if (StringUtils.isBlank(inputString)) {
            return inputString;
        }

        String htmlStr = inputString;
        String textStr = "";
        java.util.regex.Pattern p_script;
        java.util.regex.Matcher m_script;
        java.util.regex.Pattern p_style;
        java.util.regex.Matcher m_style;
        java.util.regex.Pattern p_html;
        java.util.regex.Matcher m_html;

        try {
            // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>}
            String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";

            // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>}
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";

            // 定义HTML标签的正则表达式
            String regEx_html = "<[^>]+>";

            p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
            m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); // 过滤script标签

            p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
            m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); // 过滤style标签

            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); // 过滤html标签

            textStr = htmlStr;

        } catch (Exception e) {
            System.err.println("Html2Text: " + e.getMessage());
            return inputString;
        }

        return textStr; // 返回文本字符串
    }

    public static String Text2Text(String inputString) {
        if (StringUtils.isBlank(inputString)) {
            return inputString;
        }

        try {
            inputString = inputString.replaceAll("\\[|\\]", "");
            inputString = inputString.replaceAll("【|】", "");
            inputString = inputString.replaceAll("\\\n|\r|\t", "");
            inputString = inputString.replaceAll("\\ ", "");
            inputString = inputString.replaceAll("\"", "");
            inputString = inputString.trim();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return inputString;
    }

    public static String filterText(String inputString) {
        if (StringUtils.isBlank(inputString)) {
            return inputString;
        }

        try {
            inputString = inputString.replaceAll("\\\n|\r|\t", "");
            inputString = inputString.trim();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return inputString;
    }

    // 优化行程格式
    public static String filterTravel(String travel) {
        if (StringUtils.isBlank(travel)) {
            return travel;
        }

        //剔除时间
        travel = travel.replace("：", ":");
        String regex = "(([0-1]?\\d)|(2[0-4])):[0-5]?\\d";    //时间正则：08:35
        travel = travel.replaceAll(regex, "");

        //剔除括号内容
        travel = travel.replace("（", "(").replace("）", ")");
        regex = "\\(.*?\\)";
        travel = travel.replaceAll(regex, "");

        return travel;
    }

    // 优化掉括号里面的内容
    public static String filterBrackets(String text) {
        if (StringUtils.isBlank(text)) {
            return text;
        }

        text = text.replace("（", "(").replace("）", ")");
        String regex = "\\(.*?\\)";
        text = text.replaceAll(regex, "");

        return text;
    }

    // 师傅/老师/站长/主任的都不能保存
    public static Boolean judgeUsername(String name) {
        if (StringUtils.isBlank(name)) {
            return false;
        }

        if (name.length() == 1) {
            return false;
        }

        if (name.length() == 2 && (name.contains("总") || name.contains("工"))) {
            return false;
        }

        List<String> filterName = new ArrayList<>();
        filterName.add("师傅");
        filterName.add("校长");
        filterName.add("老师");
        filterName.add("站长");
        filterName.add("主任");
        filterName.add("书记");
        filterName.add("处长");
        filterName.add("个人");
        filterName.add("先生");
        filterName.add("女士");
        filterName.add("小姐");
        filterName.add("经理");

        for (String item : filterName) {
            if (name.length() <= 3 && name.contains(item)) {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(judgeUsername("李") ? "1" : "0");
        System.out.println(judgeUsername("李主任") ? "1" : "0");
        System.out.println(judgeUsername("站长") ? "1" : "0");
    }
}
